window.onload = function() {
	seajs.use(['jquery-1.8.2.js', 'iscroll_module.js', 'mdialog_module.js', 'util_module.js', 'imgUpload_module.js'], function(Z, I, M, U, IMG) {
		var curPage = 1;
		var pageSize = 2;
		var totalPage = 0;
		var nextPage = true;
		var menu_index = 0;
		$(".left>div").unbind("click").click(function() {
			if($(this).find("span").text() == "与我相关") {
				if(!checklogin()) {
					return false;
				}
			}
			var index = $(this).index();
			menu_index = index;
			curPage = 1;　
			if(index == 0) {
				$(".publish").show(200);
				$(".left>div").eq(0).find("img").attr("src", "../images/icons/icon_luntan1.png");
				$(".left>div").eq(2).find("img").attr("src", "../images/icons/icon_xiangguan.png");
				load_dynamic_list("/active/list", {});
			} else {
				$(".publish").hide();
				$(".left>div").eq(2).find("img").attr("src", "../images/icons/icon_xiangguan1.png");
				$(".left>div").eq(0).find("img").attr("src", "../images/icons/icon_luntan.png");
				load_dynamic_list("/active/byUid", {});
			}
			$(".left>div").css("border-color", "white");
			$(".left>div").css("color", "black");
			$(this).css("border-color", "#E03C43");
			$(this).css("color", "#E03C43");
		});
		var user = U.checkLogin();
		var Reply = null;
		U.fillHead();
		initPage();

		function checklogin() {
			U.checkLogin();
			var is_login = true;
			if(user.state != 1) {
				is_login = false;
				alert("请先登录");
				U.loadLogin(true);
			}
			return is_login;
		}
		//检查图片是否上限
		function checkcount() {
			var nine = false;
			var length = $(".p_img_list>div").length;
			$(".imgs_count>span").text("图片(" + length + "/9)");
			if(length >= 9) {
				$(".img_count_tips").show();
				nine = true;
			} else {
				$(".img_count_tips").hide();
			}
			return nine;
		}

		function initPage() { //初始化页面
			U.loadOpt(false);
			$(".p_img_list").empty();
			$(".img_count_tips").hide();
			$(".address_tips").hide();
			$(".left>div").eq(0).click();
			if(U.isIE()) {
				$(".tips").show();
			}
			$(window).scroll(function() {　　
				var scrollTop = $(this).scrollTop();　　
				var scrollHeight = $(document).height();　　
				var windowHeight = $(this).height();　　
				if(scrollTop + windowHeight >= scrollHeight - 1) {　
					if(menu_index == 0) {
						if(nextPage) {
							curPage++;　
							load_dynamic_list("/active/list", {}, "clear");
						}
					} else {
						if(nextPage) {
							curPage++;　　
							load_dynamic_list("/active/byUid", {}, "clear");
						}
					}
				}
			});
		}

		function load_dynamic_list(interfaces, params, clear) {
			params = {
				curPage: curPage,
				pageSize: pageSize
			}
			U.loadOpt(true);
			U.ajaxAsync(interfaces, params, function(json) {
				U.loadOpt(false);
				if(clear == undefined) {
					$(".dynamic_list").empty();
				}
				if(json.success) {
					nextPage = json.models.page.nextPage;
					//遍历动态集合
					$(json.models.activeList).each(function() {
						var dynamic_del = "";
						if(user.state == 1) {
							if(user.info.id == this.userId) {
								dynamic_del = "<div class='dynamic_del'>删除</div>";
								if(interfaces == "/active/byUid") {
									$(".dynamic .left>div>div>div").hide();
								} else {
									$(".dynamic .left>div>div>div").show();
								}
							}
						}
						var person = new Object;
						try {
							person = this.userVO.personVO;
						} catch(e) {
							person.headImage = "";
							person.name = "未知";
						}
						var dyimgs = ""
						var input = "<span class='Reply_input'><div><input type='text' placeholder='我有话说'></div><div><div class='select_bq'><img src='../images/icons/icon_biaoqing.png'></div><div class='send'>发送</div></div></span>";
						var Reply_content = "";
						//遍历评论集合
						$(this.commentList).each(function() {
							var replyVO = "";
							//遍历回复集合
							$(this.replyList).each(function() {
								var headImage = "../images/icons/icon_unknow.png";
								if(U.getImg(this.headImage).length > 0) {
									headImage = U.getImg(this.headImage)[0];
								}
								replyVO += "<div><div><img  src='" + headImage + "'></div><div><div>" + this.name + "</div><div>" + U.getMyDate(this.gmtCreate, 16) + "</div></div><div class='Reply_text'>" + this.content + "</div><div class='Reply_me'></div></div>";
							});

							var headImage = "../images/icons/icon_unknow.png";
							if(U.getImg(this.headImage).length > 0) {
								headImage = U.getImg(this.headImage)[0];
							}
							Reply_content += "<div class='Reply_content'><div><img commentId='" + this.id + "' userName='" + this.name + "' userId='" + this.userId + "' src='" + headImage + "'></div><div><div>" + this.name + "</div><div>" + U.getMyDate(this.gmtCreate, 16) + "</div></div><div class='Reply_text'>" + this.content + "</div><div class='Reply_me'><div class='Reply_view'>" + replyVO + "</div></div></div>";
							var xx = "<div class='Reply_content'><div><img commentId='" + this.id + "' userName='" + this.name + "' userId='" + this.userId + "' src='" + headImage + "'></div><div><div>" + this.name + "</div><div>" + U.getMyDate(this.gmtCreate, 16) + "</div></div><div class='Reply_text'>" + this.content + "</div><div class='Reply_me'><div class='Reply_view'>" + replyVO + "</div></div></div>";
						});
						$(this.images.split(",")).each(function() {
							if(this != "") {
								dyimgs += "<div><img class='upload_img' src='" + this + "'> <img class='del' src='../images/icons/icon_shanchu.png'></div>";
							}
						});
						var user_list = "";
						$(this.personList).each(function() {
							var headImage = "../images/icons/icon_unknow.png";
							if(U.getImg(this.headImage).length > 0) {
								headImage = U.getImg(this.headImage)[0];
							}
							user_list += "<img userId='" + this.userId + "' src='" + headImage + "'> ";
						});
						var person_headImage = "../images/icons/icon_unknow.png";
						if(U.getImg(person.headImage).length > 0) {
							person_headImage = U.getImg(person.headImage)[0];
						}
						var dy = "<div id='" + this.id + "'>" + dynamic_del + "<div class='user_info'><div class='head_img'><img src='" + person_headImage + "'></div><div class='other_info'><div class='user_name'>" + person.name + "</div><div class='time'>" + U.getMyDate(this.gmtCreate, 16) + "</div><div class='address'><img src='../images/icons/icon_weizhi.png'>" + this.address + "</div></div></div><div class='dynamic_text'>" + this.content + "</div><div class='dynamic_imgs'>" + dyimgs + "</div><div class='praise_list'><div><div><div class='zan_btn'><img src='../images/icons/icon_dianzan.png'></div><div class='user_list'>" + user_list + "</div><div class='more_user'>· · ·更多</div></div></div></div><div class='Reply_view'>" + Reply_content + "</div>" + input + "</div>";
						$(".dynamic_list").append(dy);
					});

				} else {
					M.toast(false, json.errorMsg);
				}
				initEvent();
			});

		}

		function initEvent() {

			U.placeholder_imitate();
			$(".p_bq,.select_bq").unbind("click").click(function() {
				alert("暂时不支持表情发送!");
			});
			zan_list_width();
			$(".p_content img").unbind("click").click(function() {
				if(U.isIE()) {
					alert("请使用IE专用图片上传按钮");
					return false;
				}
				if(!checklogin()) {
					return false;
				}
				if(checkcount()) {
					alert("图片上传数量已达上限");
					return false;
				}
				$("#dynamic_img").click();
			});
			$(".publish_btn").unbind("click").click(function() {
				if(!checklogin()) {
					return false;
				} else {
					var p_content = $(".p_content textarea").val() + "";
					if(p_content == "") {
						alert("发布内容不能为空!")
						return false;
					}
					var images = "";
					$(".p_img_list>div").each(function() {
						images += $(this).find(".upload_img").attr("src") + ",";
					});
					var params = {
						userId: user.info.id,
						images: images,
						content: p_content,
						address: "未知"
					};
					U.ajax("/active/add", params, function(json) {
						if(json.success) {
							alert("发布成功!");
							location.reload();
						} else {
							M.toast(false, json.errorMsg);
						}
					});
				}
			});
			$(".dynamic_del").unbind("click").click(function() {
				var activity = $(this).parent();
				U.ajaxget("/active/del/" + $(activity).attr("id"), {}, function(json) {
					if(json.success) {
						alert("删除成功");
						$(activity).remove();
					} else {
						M.toast(false, json.errorMsg);
					}
				});
			});
			$(".dynamic_list>div .zan_btn").unbind("click").click(function() {
				if(!checklogin()) {
					return false;
				}
				var praise = true;
				var activity_id = $(this).parent().parent().parent().parent().attr("id");
				var tempUser = new Object();
				var user_list = $(this).parent().find(".user_list");
				$(this).parent().find(".user_list>img").each(function() {
					if(user.info.id == $(this).attr("userId")) {
						tempUser = this;
						praise = false;
					}
				});
				if(praise) {
					U.ajaxget("/active/praise/" + activity_id, {}, function(json) {
						if(json.success) {
							alert("已赞");
							$(user_list).append("<img userId='" + user.info.id + "' src='" + user.info.personVO.headImage + "'/>");
							zan_list_width();
						} else {
							M.toast(false, json.errorMsg);
						}
					});
				} else {
					U.ajaxget("/active/unpraise/" + activity_id, {}, function(json) {
						if(json.success) {
							alert("已取消赞");
							$(tempUser).remove();
							zan_list_width();

						} else {
							M.toast(false, json.errorMsg);
						}
					});
				}
			});
			$(".dynamic_list>div .head_img").unbind("click").click(function() {
				Reply = null;
				$(this).parent().parent().find("input").attr("placeholder", "我有话说");
			});
			$(".Reply_view img").unbind("click").click(function() {
				if(user.state != 1) {
					return false;
				}
				if($(this).attr("userId") == null || $(this).attr("userId") == user.info.id) {
					return false;
				}
				Reply = new Object();
				Reply.userId = $(this).attr("userId");
				Reply.name = $(this).attr("userName");
				Reply.commentId = $(this).attr("commentId");
				Reply.input = $(this).parent().parent().parent().parent().find(".Reply_input>div>input");
				Reply.Reply_me = $(this).parent().parent().find(".Reply_me").eq(0).find(".Reply_view");
				Reply.type = 1;
				$(Reply.input).val("");
				$(Reply.input).attr("placeholder", "@" + Reply.name + "");
			});
			$(".p_img_list>div").width(($(".dynamic .right").width() - 62 - 30 * 4) / 3);
			$(".dynamic_imgs>div").width(($(".dynamic .right").width() - 62 - 30 * 4) / 3);
			$(".p_img_list>div").height(($(".dynamic .right").width() - 62 - 30 * 4) / 3);
			$(".dynamic_imgs>div").height(($(".dynamic .right").width() - 62 - 30 * 4) / 3);
			$(".user_list").width($(".praise_list>div").width() - $(".zan_btn").width() - $(".more_user").width() - 10);
			$(".Reply_input>div:nth-child(1)").width($(".Reply_input").width() - $(".Reply_input>div:nth-child(2)").width() - 5);
			$(".more_user").unbind("click").click(function() {
				if($(this).text() == "· · ·更多") {
					if($(this).parent().find(".user_list>img").length > 0) {
						$(this).parent().find(".user_list").css("overflow", "auto");
						$(this).parent().find(".user_list").css("height", "auto");
					}
					$(this).text(" ^ 收起");
				} else {
					$(this).parent().find(".user_list").css("overflow", "hidden");
					$(this).parent().find(".user_list").css("height", "50px");
					$(this).text("· · ·更多");
				}
			});
			$(".p_img_list .del").unbind("click").click(function() {
				$(this).parent().remove();
				checkcount();
			});
			$(".send").unbind("click").click(function() {
				var Reply_view = $(this).parent().parent().parent().find(".Reply_view").eq(0);
				if(!checklogin()) {
					return false;
				}
				var input = $(this).parent().parent().parent().find(".Reply_input>div>input");
				var text = $(input).val() + "";
				if(text == "") {
					M.toast(false, "评论内容不能为空!");
					return false;
				}
				var params = {
					activeId: $(this).parent().parent().parent().attr("id"),
					commentId: user.info.id,
					content: text,
					name: user.info.username
				}
				if(Reply != null) {
					params["commentId"] = Reply.commentId;
					params["content"] = user.info.username + "回复" + Reply.name + "：" + text;
					U.ajaxAsync("/active/reply", params, function(json) {
						if(json.success) {
							alert("回复成功!");
							var replyVO = "<div><div><img  src='" + user.info.personVO.headImage + "'></div><div><div>" + Reply.name + "</div><div>" + U.getMyDate(new Date(), 16) + "</div></div><div class='Reply_text'>" + params["content"] + "</div><div class='Reply_me'></div></div>";
							$(Reply.Reply_me).append(replyVO);
							$(input).val("");
							$(input).attr("placeholder", "我有话说");
							Reply = null;
						} else {
							M.toast(false, json.errorMsg);
						}
					})
					return false;
				}
				U.ajaxAsync("/active/comment", params, function(json) {
					if(json.success) {
						alert("评论成功!");
						var commentVO = json.models.commentVO;
						var Reply_content = "<div class='Reply_content'><div><img commentId='" + commentVO.id + "' userName='" + user.info.username + "' userId='" + user.info.id + "' src='" + user.info.personVO.headImage + "'></div><div><div>" + user.info.username + "</div><div>" + U.getMyDate(new Date(), 16) + "</div></div><div class='Reply_text'>" + params["content"] + "</div><div class='Reply_me'><div class='Reply_view'></div></div></div>";
						$(Reply_view).append(Reply_content);
						$(input).val("");
						$(input).attr("placeholder", "我有话说");
						initEvent();
					} else {
						M.toast(false, json.errorMsg);
					}
				})
			});
		}

		function zan_list_width() {
			$(".user_list").each(function() {
				if($(this).find("img").length > ($(this).width() / 56)) {
					$(this).parent().find(".more_user").show();
				} else {
					$(this).parent().find(".more_user").hide();
				}
			});
		}

		function registerfj() {
			IMG.ajaxFileUpload("dynamic_img", function(result) {
				if(result.success) {
					var url = result.models.url;
					$(".p_img_list").append("<div><img class='upload_img' src='" + url + "'> <img class='del' src='../images/icons/icon_shanchu.png'></div>");
					checkcount();
					initEvent();
					$("#dynamic_img").replaceWith("<input type='file'  name= 'dynamic_img' id='dynamic_img'>");
					registerfj();
				}
			});
		}
		if(U.isIE()) {
			$("#dynamic_img").show();
			registerfj();
		} else {
			IMG.uploadImg({
				'obj': $('#dynamic_img'),
				'success': function(json, obj) {
					U.loadOpt(false);
					if(json.state == "SUCCESS") {
						var url = json.url;
						$(".p_img_list").append("<div><img class='upload_img' src='" + url + "'> <img class='del' src='../images/icons/icon_shanchu.png'></div>");
						checkcount();
						initEvent();
					} else {
						alert("上传失败!");
					}
				}
			});
		}
	});
}